var yourIP = "888.888.888.888";
var dir = "/sdcard/koaDownload";
var fileList = getfileList();
var fileList = replaceIP(yourIP, fileList);
downloadfileList(fileList);

function getfileList() {
  var url = "http://" + yourIP + ":3000/fileList";
  var r = http.get(url);
  r = r.body.string();
  log(r);
  r = JSON.parse(r);
  return r;
}
function replaceIP(yourIP, fileList) {
  var len = fileList.length;
  for (var i = 0; i < len; i++) {
    var file = fileList[i];
    console.log("替换IP前 file=" + fileList[i]);
    fileList[i] = file.replace(/\d{3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/, yourIP);
    console.log("替换IP后 file=" + fileList[i]);
  }
  return fileList;
}
function getFilePath(url) {
  // 127.0.0.1:3000/download?path=/users.js
  var filePath = url.split("path=/")[1];
  console.log(filePath);
  return filePath;
}
function downloadfileList(fileList) {
  var len = fileList.length;
  for (var i = 0; i < len; i++) {
    var url = fileList[i];
    console.log(url);
    var filePath = getFilePath(url);
    filePath = files.join(dir, filePath);
    files.createWithDirs(filePath);
    var r = http.get(url);
    files.writeBytes(filePath, r.body.bytes());
  }
}
